//
//  NSString+Helper.m
//  ARErp
//
//  Created by ZRAR on 14/11/6.
//  Copyright (c) 2014年 ZRAR. All rights reserved.
//

#import "NSString+Helper.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (Helper)

//#pragma mark 清空字符串中的空白字符
//- (NSString *)trimString
//{
//    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//}
//
//#pragma mark 是否空字符串
//- (BOOL)isEmptyString
//{
//    return (self.length == 0);
//}

#pragma mark 是否空字符串
+ (BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL||[string isEqual:[NSNull null]]) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    //去空格之后判断length是否为0
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *content = [string stringByTrimmingCharactersInSet:whitespace];
    if ([content length] == 0)
    {
        return YES;
    }
    return NO;
}



#pragma mark---转换手机号为加星号
-(NSString *)phoneNumToAsterisk;
{
    return [self stringByReplacingCharactersInRange:NSMakeRange(3,4) withString:@"****"];
}

#pragma mark---转换身份证号为加星号
-(NSString*)idCardToAsterisk
{
    return [self stringByReplacingCharactersInRange:NSMakeRange(4, 12) withString:@"********"];
}


+(NSString *)timeFormatted:(float)totalSeconds
{
    NSDate  *date = [NSDate dateWithTimeIntervalSince1970:totalSeconds];
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd  hh:mm:ss";
    NSString * dateStr = [formatter stringFromDate:date];
    
    
    return dateStr;
}
#pragma mark - MD5加密 32位 小写
- (NSString *)k_MD5String
{
    const char *string = self.UTF8String;
    int length = (int)strlen(string);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(string, length, bytes);
    
    return [self k_stringFromBytes:bytes length:CC_MD5_DIGEST_LENGTH];
}
#pragma mark - MD5加密 32位 大写
+ (NSString *)MD5ForUpper32Bate:(NSString *)str{
    //要进行UTF8的转码
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02X", result[i]];
    }
    return digest;
}

- (NSString *)k_stringFromBytes:(unsigned char *)bytes
                         length:(NSInteger)length
{
    NSMutableString *mutableString = NSMutableString.string;
    for (NSInteger i = 0; i < length; i++) {
        [mutableString appendFormat:@"%02x", bytes[i]];
    }
    return mutableString.copy;
}

+(NSString*)dictionaryToJson:(id)dic
{
    NSError*error =nil;
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONReadingMutableLeaves |NSJSONReadingAllowFragments error:&error];
    if (jsonData == nil) {
        return nil;
    }
    NSString *string = [[NSString alloc] initWithData:jsonData
                                             encoding:NSUTF8StringEncoding];
    return string;
}


// 图片转64base字符串
+ (NSString *)imageToString:(UIImage *)image
{
    NSData *imagedata = UIImageJPEGRepresentation(image, 1.0f);
    NSString *image64 = [imagedata base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return image64;
        
}
// 全为数字
+ (BOOL)inputShouldNumber:(NSString *)inputString {
    if (inputString.length == 0) return NO;
    NSString *regex =@"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:inputString];
}
// 全为字母
+ (BOOL)inputShouldLetter:(NSString *)inputString {
    if (inputString.length == 0) return NO;
    NSString *regex =@"[a-zA-Z]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:inputString];
}
//全汉字
+ (BOOL)inputShouldChinese:(NSString *)inputString {
    if (inputString.length == 0) return NO;
    NSString *regex = @"[\u4e00-\u9fa5]+";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:inputString];
}
//获取当前时间戳
+(NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYYMMddHHmmss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    NSLog(@"currentTimeString =  %@",currentTimeString);
    
    return currentTimeString;
}
+ (NSString *)getDayString:(NSString *)inputString
{
    if ([inputString containsString:@"1-2-3-4-5-6-7"]) {
        return  @"每天";
    }else if ([inputString isEqualToString:@"1-2-3-4-5"])
    {
        return  @"工作日";
    }else if ([inputString isEqualToString:@"6-7"])
    {
        return  @"周末";
    }else
    {
        NSArray * strArray = [inputString componentsSeparatedByString:@"-"];
        NSMutableArray * tempArrary = [NSMutableArray array];
        NSDictionary * dict = [NSDictionary dictionary];
        dict = @{
                 @"1" : @"星期一",
                 @"2" : @"星期二",
                 @"3" : @"星期三",
                 @"4" : @"星期四",
                 @"5" : @"星期五",
                 @"6" : @"星期六" ,
                 @"7" : @"星期天" ,
                 };
        for (int i =0; i<strArray.count; i++) {
            [tempArrary addObject:[dict valueForKey:strArray[i]]];
        }
        return [tempArrary componentsJoinedByString:@","];
    }
    return nil;
}

+ (NSString *)handleStrIsEmptyFrom:(NSString *)str
{
    NSString *resultStr =nil;
    if ([NSString isBlankString:str]) {
        resultStr = @"";
        return resultStr;
    }
    return str;
}


#pragma mark 判断身份证合法性
+ (BOOL)judgeIdentityCardValueUseString:(NSString *)str
{
    if (str.length!=18) {
        return NO;
    }
//    NSString * a = [str substringWithRange:NSMakeRange(0, 1)];
//    int b1 = [a intValue]*7;
//    NSString * a2 = [str substringWithRange:NSMakeRange(1, 1)];
//    int b2 = [a2 intValue]*9;
//    NSString * a3 = [str substringWithRange:NSMakeRange(2, 1)];
//    int b3 = [a3 intValue]*10;
//    NSString * a4 = [str substringWithRange:NSMakeRange(3, 1)];
//    int b4 = [a4 intValue]*5;
//    NSString * a5 = [str substringWithRange:NSMakeRange(4, 1)];
//    int b5 = [a5 intValue]*8;
//    NSString * a6 = [str substringWithRange:NSMakeRange(5, 1)];
//    int b6 = [a6 intValue]*4;
//    NSString * a7 = [str substringWithRange:NSMakeRange(6, 1)];
//    int b7 = [a7 intValue]*2;
//    NSString * a8 = [str substringWithRange:NSMakeRange(7, 1)];
//    int b8 = [a8 intValue]*1;
//    NSString * a9 = [str substringWithRange:NSMakeRange(8, 1)];
//    int b9 = [a9 intValue]*6;
//    NSString * a10 = [str substringWithRange:NSMakeRange(9, 1)];
//    int b10 = [a10 intValue]*3;
//    NSString * a11 = [str substringWithRange:NSMakeRange(10, 1)];
//    int b11 = [a11 intValue]*7;
//    NSString * a12 = [str substringWithRange:NSMakeRange(11, 1)];
//    int b12 = [a12 intValue]*9;
//    NSString * a13 = [str substringWithRange:NSMakeRange(12, 1)];
//    int b13 = [a13 intValue]*10;
//    NSString * a14 = [str substringWithRange:NSMakeRange(13, 1)];
//    int b14 = [a14 intValue]*5;
//    NSString * a15 = [str substringWithRange:NSMakeRange(14, 1)];
//    int b15 = [a15 intValue]*8;
//    NSString * a16 = [str substringWithRange:NSMakeRange(15, 1)];
//    int b16 = [a16 intValue]*4;
//    NSString * a17 = [str substringWithRange:NSMakeRange(16, 1)];
//    int b17 = [a17 intValue]*2;
//    NSString * a18 =[str substringWithRange:NSMakeRange(17, 1)];
//    int sum = b1+b2+b3+b4+b5+b6+b7+b8+b9+b10+b11+b12+b13+b14+b15+b16+b17;
//    int number = sum % 11;
//    
//    if (number==0 && (![a18 isEqualToString:@"1"])) {
//        return NO;
//    }
//    if (number==1 && (![a18 isEqualToString:@"0"])) {
//        return NO;
//    }
//    if (number==2 && (![a18 isEqualToString:@"X"])) {
//        return NO;
//    }
//    if (number==3 && (![a18 isEqualToString:@"9"])) {
//        return NO;
//    }
//    if (number==4 && (![a18 isEqualToString:@"8"])) {
//        return NO;
//    }
//    if (number==5 && (![a18 isEqualToString:@"7"])) {
//        return NO;
//    }
//    if (number==6 && (![a18 isEqualToString:@"6"])) {
//        return NO;
//    }
//    if (number==7 && (![a18 isEqualToString:@"5"])) {
//        return NO;
//    }
//    if (number==8 && (![a18 isEqualToString:@"4"])) {
//        return NO;
//    }
//    if (number==9 && (![a18 isEqualToString:@"3"])) {
//        return NO;
//    }
//    if (number==10 && (![a18 isEqualToString:@"2"])) {
//        return NO;
//    }
    return YES;
}

@end





